import os
from service.Message import  Message,CodeMessageData


class FileHelper():
    #更Xlshelper中的表格顺序一样
    __tableList = ['','tb_personal_team', 'tb_exchange', 'tb_subject', 'tb_project', 'tb_natural_science', 'tb_patent',
                 'tb_paper', 'tb_book']
    __tableChange = ['','个人或团队荣誉', '培训进修', '学科竞赛', '项目', '教学科研奖', '专利', '论文', '著作']
    __fileSaveDir='./file'

    def __init__(self):
        print("FileHelper init sucess")

    def getFileSaveDir(self):
        """
        :return: fileSaveDir
        """
        return self.__fileSaveDir


    def makedirs(self,path):
        """
        :param path: file save directory
        :return:    Message Object
        """
        code=0000
        try:
            if os.path.exists(path):
                pass
            else:
                os.makedirs(path)
        except:
            code=1034
        return  Message(code,CodeMessageData[code])


    def getName(self,nameList,saveType='.zip'):
        """
        :param nameList:name list .
        :param saveType:save file type. default zip format
        :return: Message Object
        """
        code=0000
        data=[]

        try:
            nameList = [str(one) for one in nameList]
            name = "_".join(nameList) + saveType
            data.append(name)
        except:
            code=1035
        return  Message(code,CodeMessageData[code],data=data)

    def saveFile(self,path, name, file):
        """
        :param path: file save directory
        :param name: file name
        :param file: file stream
        :return: Message Object
        """
        try:
            code=0000
            savePath = os.path.join(path, name)
            with open(savePath, "wb") as f:
                f.write(file)
        except Exception as ex:
            string=str(ex)
            code=1036
        return Message(code,CodeMessageData[code])

    def removedirs(self,path):
        try:
            code=0000
            os.removedirs(path)
        except:
            code=1037
        return  Message(code,CodeMessageData[code])

    def getPath(self,dirList):
        path = ''
        for one in dirList:
            path = os.path.join(path, str(one))
        return path

    def getEngName(self,excelType):
        """

        :param excelType: excel type
        :return: table English name
        """
        return self.__tableList[excelType]

    def getChiName(self,excelType):
        """
        :param excel type:
        :return: table Chinese name
        """
        return self.__tableChange[excelType]


    def remove(self,filePath):
        code=0000
        try:
            os.remove(filePath)
        except Exception as ex:
            value=ex.args[1]
            if(value=='No such file or directory'):
                code=0000
            else:
                code=1049
        return Message(code,CodeMessageData[code])


    def typeFileId(self,excelType,id):
        """
        :param excelType:excel type
        :param id: id
        :return:
        """
        return str(excelType)+"_"+str(id)